﻿using Tester.Models;
#if ANDROID

using Android.Content.PM;
using Android;
using Java.IO;

#endif

namespace Tester.ViewModels;

internal class MainViewModels : IViewModels
{
    public List<NavItemModel> NavList { get; set; }
    public List<Widget> Widgets { get; set; }

    public string str = "主页";

    public MainViewModels()
    {
        NavList = new List<NavItemModel>();
        NavList.Add(new NavItemModel { Icon = "\ue78c", Text = "主页" });
        NavList.Add(new NavItemModel { Icon = "\ue77a", Text = "设置" });
        NavList.Add(new NavItemModel { Icon = "\ue775", Text = "个性化" });
        NavList.Add(new NavItemModel { Icon = "\ue774", Text = "个人" });

        Widgets = new List<Widget>();
        InitializeWidget();
    }

    public void InitializeWidget()
    {
        Widgets.Add(new Widget { Header = "测试", Value1 = 0, Mode = 0, Description = "\ue72e Execute", EditButtonCommand = new Command(OnEdit) });
        Widgets.Add(new Widget { Header = "测试2", Value1 = 0, Mode = 0, Description = "\ue72e Execute", EditButtonCommand = new Command(Test) });
        Widgets.Add(new Widget { Header = "测试3", Value1 = 0, Mode = 0, Description = "\ue72e Execute", EditButtonCommand = new Command(GetStorage) });
        Widgets.Add(new Widget { Header = "测试4", Value1 = 0, Mode = 0, Description = "\ue72e Execute", EditButtonCommand = new Command(WriteSomething) });
        Widgets.Add(new Widget { Header = "导航测试", Value1 = 0, Mode = 0, Description = "\ue72e Execute", EditButtonCommand = new Command(Navito) });
    }

    private void OnEdit()
    {
        string info = string.Empty;
        info += "CurrentDirectory:" + Environment.CurrentDirectory + "\n";
        info += "ProcessPath:" + Environment.ProcessPath + "\n";
        info += "Personal:" + Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\n";
        Models.Message.Show("ShowDialog", "title", info, "ok");
    }

    private void Test()
    {
        Models.Message.Execute("ResizeWindow");
    }

    private void GetStorage()
    {
        Models.Message.Show("ShowDialog", "title", Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "OK");
    }

    private async void WriteSomething()
    {
#if ANDROID
        PermissionStatus statusread = await Permissions.RequestAsync<Permissions.StorageRead>();
        PermissionStatus statuswrite = await Permissions.RequestAsync<Permissions.StorageWrite>();
        string path = "/storage/emulated/0/Tester";
        Directory.CreateDirectory(path);
        FileStream fs = new(path + "/test.json", FileMode.Create);
        StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
        await sw.WriteAsync(str);
        sw.Close();
        fs.Close();
#elif WINDOWS
        string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\test.json";
        FileStream fs = new(path, FileMode.Create);
        StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
        await sw.WriteAsync(str);
        sw.Close();
        fs.Close();
#endif
    }

    private void Navito()
    {
        Navigate.Execute("Navigate", typeof(Tester.Views.PersonalizePage));
    }
}